//
//  ShowView.m
//  wnkdwaiter
//
//  Created by 尹桥印 on 15/3/28.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "CustomShowView.h"

@interface CustomShowView()
{
    UIButton *_bgBtn;//背景的btn，点击则把此view消失在屏幕上
    UIView *_customView;
}

@end
@implementation CustomShowView

-(instancetype)initWithCustomView:(UIView *)view leftTopPoint:(CGPoint)point{
    self = [super init];
    if (self) {
        [self setSize:CGSizeMake(SCREENW, SCREENH)];
        _bgBtn = [[UIButton alloc]initWithFrame:self.frame];
        [_bgBtn setBackgroundColor:[UIColor blackColor]];
        [_bgBtn setAlpha:0.011];
        [_bgBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_bgBtn];
        
        if (CGPointEqualToPoint(point, CGPointZero)) {
            view.center = self.center;
        }else{
            view.x = point.x;
            view.y = point.y;
        }
        _customView = view;
        [self addSubview:view];
        //添加键盘弹出与隐藏的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
    }
    return self;
}

-(instancetype)initWithCustomView:(UIView *)view{
    return [self initWithCustomView:view leftTopPoint:CGPointZero];
}


-(void)showWithanim:(BOOL)animated{
    if (!self.superview) {
        if (animated) {
            self.alpha = 0.001;
            [[UIApplication sharedApplication].keyWindow addSubview:self];
            [UIView animateWithDuration:0.25 animations:^{
                self.alpha = 1;
            }];
        }else{
            [[UIApplication sharedApplication].keyWindow addSubview:self];
        }
    }
}

-(void)hide{
    [self hideWithanim:YES];
}

-(void)hideWithanim:(BOOL)animated{
    //移除键盘监听
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
    [_bgBtn removeTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    if (animated) {
        [UIView animateWithDuration:0.25 animations:^{
            [self setAlpha:0.001];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }else{
        [self removeFromSuperview];
    }
}

- (void)keyboardWillShow:(NSNotification *)notif
{
    //在键盘显示的时候去调整view
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    //首页判断键盘有没有遮挡view，如果没有遮挡，那个什么也不做
    CGFloat viewMaxY = CGRectGetMaxY(_customView.frame);
    if (viewMaxY+10>SCREENH-keyboardSize.height) {
        //代表键盘遮挡了view
        //需要将view移动
        [UIView animateWithDuration:0.25 animations:^{
            [_customView setY:SCREENH-keyboardSize.height-_customView.height-10];
        }];
    }
}

-(void)keyboardWillHide:(NSNotification*)notif
{
    //在键盘隐藏的时候去调整view
    [UIView animateWithDuration:0.25 animations:^{
        [_customView setCenter:self.center];
    }];
}

-(void)setMalpha:(CGFloat)malpha{
    [_bgBtn setAlpha:malpha];
}

@end
